import inspect
from pydantic import conint

from common.base.base_api import BaseApi
from services.attenuators.schemas.configs import AttenuatorsConfig
from settings import settings


class AttenuatorsApi(BaseApi):
    def __init__(self):
        super(AttenuatorsApi, self).__init__(request_queue_uuid=settings.ATTENUATORS_REQUEST_QUEUE)

    async def set_attenuators(self, config: AttenuatorsConfig) -> bool | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, config.dict())

    async def get_attenuators(self) -> AttenuatorsConfig | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda r: AttenuatorsConfig(**r)
        )

    async def get_attenuator(self, index: conint(ge=0, lt=settings.ATTENUATORS_NUMBER)) -> float | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})
